<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Netty心跳检测机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-shadow {
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .card-shadow:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            color: #e6e6e6;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 16px;
            font-size: 24px;
            margin-bottom: 20px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            padding-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 30px;
            bottom: -30px;
            width: 2px;
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::before {
            display: none;
        }
        
        .timeline-dot {
            position: absolute;
            left: 0;
            top: 8px;
            width: 22px;
            height: 22px;
            border-radius: 50%;
            background: #667eea;
            border: 4px solid #f3f4f6;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fade-in {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center animate-fade-in">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    Netty心跳检测机制
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    构建高可靠性网络应用的核心技术
                </p>
                <div class="flex justify-center space-x-6 text-sm">
                    <span class="flex items-center">
                        <i class="fas fa-clock mr-2"></i>
                        阅读时间：8分钟
                    </span>
                    <span class="flex items-center">
                        <i class="fas fa-layer-group mr-2"></i>
                        技术深度：中级
                    </span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="max-w-5xl mx-auto mb-12">
            <div class="bg-white rounded-2xl card-shadow p-8 md:p-12">
                <p class="text-lg leading-relaxed text-gray-700 first-letter:text-6xl first-letter:font-bold first-letter:text-gradient first-letter:float-left first-letter:mr-3">
                    在Netty中，心跳检测是一种用来检查网络连接是否仍然有效的机制，特别是在长时间保持连接的场景中（如WebSocket、长连接的TCP协议等）。心跳检测帮助检测和处理因网络问题导致的连接中断或失效，从而提高应用的可靠性和稳定性。
                </p>
            </div>
        </div>

        <!-- Core Concepts Visualization -->
        <div class="max-w-5xl mx-auto mb-12">
            <div class="bg-white rounded-2xl card-shadow p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-8 text-gray-800">
                    <i class="fas fa-diagram-project mr-3 text-gradient"></i>
                    心跳检测架构图
                </h2>
                <div class="mermaid">
                    graph TB
                        A[客户端] -->|建立连接| B[Netty服务器]
                        B --> C{IdleStateHandler}
                        C -->|读空闲检测| D[READER_IDLE事件]
                        C -->|写空闲检测| E[WRITER_IDLE事件]
                        C -->|读写空闲检测| F[ALL_IDLE事件]
                        D --> G[触发心跳处理]
                        E --> G
                        F --> G
                        G -->|发送心跳包| A
                        A -->|响应心跳| B
                        G -->|超时未响应| H[关闭连接]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                        style G fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                        style H fill:#fa709a,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>

        <!-- Working Principle Section -->
        <div class="max-w-5xl mx-auto mb-12">
            <div class="bg-white rounded-2xl card-shadow p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-8 text-gray-800">
                    <i class="fas fa-cogs mr-3 text-gradient"></i>
                    心跳检测的工作原理
                </h2>
                
                <div class="highlight-box mb-8">
                    <p class="text-gray-700 leading-relaxed">
                        心跳检测通常通过定期发送心跳消息或进行连接检查来实现。Netty提供了<code class="bg-gray-100 px-2 py-1 rounded text-purple-600 font-mono">IdleStateHandler</code>，可以帮助实现连接的心跳检测。<code class="bg-gray-100 px-2 py-1 rounded text-purple-600 font-mono">IdleStateHandler</code>会根据配置的时间间隔，监控连接的闲置状态并触发事件。
                    </p>
                </div>

                <div class="grid md:grid-cols-3 gap-6">
                    <div class="text-center">
                        <div class="feature-icon bg-blue-100 text-blue-600 mx-auto">
                            <i class="fas fa-heartbeat"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3">定期检测</h3>
                        <p class="text-gray-600">按照预设的时间间隔自动检测连接状态</p>
                    </div>
                    <div class="text-center">
                        <div class="feature-icon bg-purple-100 text-purple-600 mx-auto">
                            <i class="fas fa-sync-alt"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3">事件驱动</h3>
                        <p class="text-gray-600">基于Netty的事件模型，高效处理空闲事件</p>
                    </div>
                    <div class="text-center">
                        <div class="feature-icon bg-pink-100 text-pink-600 mx-auto">
                            <i class="fas fa-shield-alt"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3">可靠保障</h3>
                        <p class="text-gray-600">及时发现并处理失效连接，保证系统稳定性</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Configuration Section -->
        <div class="max-w-5xl mx-auto mb-12">
            <div class="bg-white rounded-2xl card-shadow p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-8 text-gray-800">
                    <i class="fas fa-tools mr-3 text-gradient"></i>
                    配置和使用心跳机制
                </h2>

                <h3 class="text-2xl font-semibold mb-6 text-gray-700">
                    使用 <code class="text-purple-600 font-mono">IdleStateHandler</code>
                </h3>

                <p class="text-gray-700 mb-6 leading-relaxed">
                    <code class="bg-gray-100 px-2 py-1 rounded text-purple-600 font-mono">IdleStateHandler</code>是Netty中用于处理连接闲置的处理器。它根据指定的时间间隔，触发不同的闲置状态事件（如读空闲、写空闲、读写空闲）。通过这些事件，可以在应用层实现心跳检测逻辑。
                </p>

                <div class="mb-8">
                    <h4 class="text-xl font-semibold mb-4 text-gray-700">
                        <i class="fas fa-list-ol mr-2 text-purple-500"></i>
                        配置步骤
                    </h4>
                    
                    <div class="timeline-item">
                        <div class="timeline-dot"></div>
                        <h5 class="text-lg font-semibold mb-3">添加 IdleStateHandler 到 ChannelPipeline 中</h5>
                        
                        <div class="code-block">
                            <div class="code-header">
                                <div class="code-dots">
                                    <span class="dot dot-red"></span>
                                    <span class="dot dot-yellow"></span>
                                    <span class="dot dot-green"></span>
                                </div>
                                <span>MyChannelInitializer.java</span>
                            </div>
                            <pre><code>import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class MyChannelInitializer extends ChannelInitializer&lt;SocketChannel&gt; {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        
        // 添加IdleStateHandler，检测读写空闲
        pipeline.addLast(new IdleStateHandler(60, 30, 0)); // 60秒读空闲，30秒写空闲

        // 自定义处理心跳事件
        pipeline.addLast(new SimpleChannelInboundHandler&lt;Object&gt;() {
            @Override
            protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent